/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.display;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;

import com.android.tv.settings.R;
import com.android.tv.settings.dialog.DialogFragment;
import com.android.tv.settings.dialog.DialogFragment.Action;
import android.os.SystemProperties;
import com.android.tv.settings.tvos.DlpSettingsDialog;
import com.android.tv.settings.tvos.DlpSettingsController;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import java.util.ArrayList;

/**
 * Activity that allows the enabling and disabling of ProjMode effects.
 */
public class KeyStoneActivity extends Activity implements Action.Listener {

    private static final String PREFERENCE_KEY = "display";
    private static final String ACTION_AUTO_KEYSTONE  = "autokeystone_1";
	private static final String ACTION_MAN_KEYSTONE   = "mankeystone_2";
	private static final String ACTION_DEF_KEYSTONE   = "mankeystone_0";
	private DlpSettingsDialog mDlpSettingsDialog;
    private DialogFragment mDialogFragment;
	private Handler mHandler;
	ArrayList<Action> mActions;
	
	private static final int AUTOSTONE0 = 999;
	private static final int AUTOSTONE1 = 998;
	private static final int AUTOSTONE2 = 997;
	private static final int MANSTONE0	 = 996;
	private static final int MANSTONE1  = 995;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDialogFragment = new DialogFragment.Builder()
                .title(getString(R.string.proj_keystone))
                .breadcrumb(getString(R.string.device_display))
                .iconResourceId(getIconResource(getContentResolver()))
                .iconBackgroundColor(getResources().getColor(R.color.icon_background))
                .actions(getActions()).build();
        DialogFragment.add(getFragmentManager(), mDialogFragment);
		mHandler = new Handler()
		{
	        @Override
	        public void handleMessage(Message msg) {
           		switch(msg.what)
           		{
					case AUTOSTONE0:
						DlpSettingsController.setAutoKeyStone("0");
						break;
						
					case AUTOSTONE1:
						DlpSettingsController.setAutoKeyStone("1");
						break;
					case AUTOSTONE2:
						DlpSettingsController.setAutoKeyStone("2");
						break;
					case MANSTONE0:
						DlpSettingsController.setManKeyStone("0");
						 {
						  	Action mAction = findActions(ACTION_MAN_KEYSTONE);
							if (mAction != null)
							{
								String mvalue = DlpSettingsController.getManKeyStone( );
								int value = 0;
								if (mvalue != null)
								{
									try
									{
										value = Integer.parseInt(mvalue);
									} 
									catch (NumberFormatException e)
									{
										e.printStackTrace();
									}
								}
								mAction.setDescription(String.valueOf(value));
								mDialogFragment.reFreshData();
							}
                       }
						break;
						
					case MANSTONE1:
						
						break;
				}
				super.handleMessage(msg);
        	}
		};
    }

    private ArrayList<Action> getActions() {
        String ProjModeAreOn = DlpSettingsController.getAutoKeyStone( );
        mActions = new ArrayList<Action>();
        mActions.add(new Action.Builder()
                .key(ACTION_AUTO_KEYSTONE)
                .title(getString(R.string.proj_autokeystone))
                .checked(ACTION_AUTO_KEYSTONE.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
		mActions.add(new Action.Builder()
                .key(ACTION_MAN_KEYSTONE)
                .title(getString(R.string.proj_mkeystone))
                .checked(ACTION_MAN_KEYSTONE.contains(ProjModeAreOn))
                .checkSetId(1)
                .description(DlpSettingsController.getManKeyStone())
                .build());
		mActions.add(new Action.Builder()
						.key(ACTION_DEF_KEYSTONE)
						.title(getString(R.string.proj_defkeystone))
						.checked(ACTION_DEF_KEYSTONE.contains(ProjModeAreOn))
						.checkSetId(1)
						.build());
        return mActions;
    }

	private Action findActions(String mKey) {
		Action mAction = null;
		for(int i = 0; i < mActions.size();i++)
		{
			mAction = (Action)mActions.get(i);
			if (mKey.equals(mAction.getKey()))
			{
				return mAction;
			}
		}
		return null;
    }	

    @Override
    public void onActionClicked(Action action) {
        if (ACTION_AUTO_KEYSTONE.equals(action.getKey()))
		{
			mHandler.sendEmptyMessage(AUTOSTONE1);
			mHandler.sendEmptyMessageDelayed(MANSTONE0,800);//800ms
        }
		else if (ACTION_DEF_KEYSTONE.equals(action.getKey()))
		{
			mHandler.sendEmptyMessage(AUTOSTONE0);
			mHandler.sendEmptyMessageDelayed(MANSTONE0,800);//800ms
        }
		else if (ACTION_MAN_KEYSTONE.equals(action.getKey()))
		{
			mHandler.sendEmptyMessage(AUTOSTONE2);			
			mDlpSettingsDialog = new DlpSettingsDialog(this, DlpSettingsController.MANKEYSTONE);
			mDlpSettingsDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
               @Override
               public void onDismiss(DialogInterface dialog) {
                   mHandler.post(new Runnable() {
                       @Override
                       public void run() {
						  	Action mAction = findActions(ACTION_MAN_KEYSTONE);
							if (mAction != null)
							{
								String mvalue = DlpSettingsController.getManKeyStone( );
								int value = 0;
								if (mvalue != null)
								{
									try
									{
										value = Integer.parseInt(mvalue);
									} 
									catch (NumberFormatException e)
									{
										e.printStackTrace();
									}
								}
								mAction.setDescription(String.valueOf(value));
								mDialogFragment.reFreshData();
							}
                       }
                   });
                   mDlpSettingsDialog = null;
               }
           });
           mDlpSettingsDialog.show( );
		}
        mDialogFragment.setIcon(getIconResource(getContentResolver()));
    }

    public static String getPreferenceKey() {
        return PREFERENCE_KEY;
    }

    public static int getIconResource(ContentResolver contentResolver) {
        return R.drawable.ic_settings_display;//need to do replace ico Blues
    }

	public static int getKeyStoneDesc( ) {
        String tmp =  DlpSettingsController.getAutoKeyStone( );
		if (tmp.equals("0"))
		{
			return R.string.proj_defkeystone;
		}
		else if (tmp.equals("1"))
		{
			return R.string.proj_autokeystone;
		}
		else if (tmp.equals("2"))
		{
			return R.string.proj_mkeystone;
		}
		return R.string.proj_defkeystone;
    }
}
